/**
 * @author raelzhu
 * @version $Revision: 1.12 $
 */
Opal.Util.registerEventListener = function register_event_listener(eventTarget, eventType, eventListener) {
	if (typeof eventListener != "function") return false;
	if (eventTarget.tagName) {
		Event.observe(eventTarget, eventType, eventListener.bindAsEventListener(eventTarget));
		return true;
	} else if (eventTarget.addEventListener) {
		eventTarget.addEventListener(eventType, eventListener, false);
		return true;
	} else if (eventTarget.attachEvent) {
		return eventTarget.attachEvent('on' + eventType, eventListener);
	} else {
		return false;
	}
}

Opal.Util.populateContext = function populate_context(expression, value) {
	$populate_context(expression, value);
}

Opal.Util.getParentNodeByTagName = function get_parent_by_tagname(obj, name) {
	var parentObj = obj.parentNode;
	if (!parentObj) return null;
	if (parentObj.tagName && parentObj.tagName.toLowerCase() == name) {
		return parentObj;
	}
	return get_parent_by_tagname(parentObj, name);
}

Opal.Util.redirectTo = function redirect_to(o) {
	if (typeof o == 'string') {  // for compilance
		location.replace(o);
		return;
	}
	if (!o.url) {
		throw 'url is missing!';
	}
	var params = o.params || {};
	if (o.isRandom != false || o.url == 'about:blank') {
		params.o_thread = Math.random();
	}
	var paramString = $H(params).toQueryString();
	var url = o.url + ((paramString != '') ? '?' + paramString : '');
	var target = o.target || window;
	target.location.replace(url);
}

Opal.Util.popupWindow = function popup_window(o) {
	if (typeof o == 'string') {  // for compilance
		var tmp = o;
		o = {url : tmp};
	}
	if (!o.url) {
		throw 'url is missing!';
	}
	var params = o.params || {};
	if (o.isRandom != false) {
		params.o_thread = Math.random();
	}
	var paramString = $H(params).toQueryString();
	var winURL = o.url + ((paramString != '') ? '?' + paramString : '');
	var winName = o.name || 'newWindow';
	var winFeature = $H(Object.extend({
		left: 100,
		top: 100,
		menubar: 'no',
		toolbar: 'no',
		location: 'no',
		status: 'no',
		resizable: 'yes',
		scrollbars: 'yes',
		width: 640,
		height: 480
	}, o.feature || {})).toQueryString().replace(/&/g, ',');
	return window.open(winURL, winName, winFeature);
}

Opal.Util.showMessage = function show_message(o) {
	if (typeof o == 'string') {
		alert(o);
	}
}

Opal.Util.showConfirm = function show_confirm(o) {
	if (typeof o == 'string') {
		return confirm(o);
	}
}

Opal.Util.showDialog = function show_dialog(o) {
	new lightbox(o);
}

Opal.Util.addBeforeValidator = function add_before_validator(o){
	var groupId = o.groupId;
	var ruleName = o.ruleName;
	var value = o.value;
	var group = Opal.Validator.Group.getGroup(groupId);
	group.unshift({type:ruleName, value:value});
}

Opal.Util.addAfterValidator = function add_after_validator(o){
	var groupId = o.groupId;
	var ruleName = o.ruleName;
	var value = o.value;
	var group = Opal.Validator.Group.getGroup(groupId);
	group.push({type:ruleName, value:value});
}

/************************************************************/
Opal.Util._setCursor = function _set_cursor(element) {
	var parentId = element.getAttribute('parent');
	if (parentId) {
		var currentRow = Opal.Util.getParentNodeByTagName(element, 'tr');
		var index = currentRow.getAttribute('idxRow');
		eval('context.' + $O(parentId).binding.item + '= context.' + $O(parentId).binding.items + '[' + index + ']');
	}
}

Opal.Util.Assert = {};

Opal.Util.Assert.isEmpty = function isEmpty(value) {
	if (typeof value == 'undefined') {
		return true;
	}
	if (value === null) {
		return true;
	}
	return value.toString().length == 0 ? true:false;
};

Assert = {};
Assert.isEmpty = Opal.Util.Assert.isEmpty;

Opal.Util.Assert.isNotEmpty = function isNotEmpty(value) {
	return !Opal.Util.Assert.isEmpty(value);
};

Assert.isNotEmpty = Opal.Util.Assert.isNotEmpty;
